package com.example.HelloSpace;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.HelloSpace.utils.MyApp;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.example.HelloSpace.adapters.MainViewPagerAdapter;


public class MainFragment extends Fragment {

    private ViewPager2 viewPager;
    private TabLayout tabLayout;
    private View rootView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        // Inflate the layout for this fragment
        rootView=inflater.inflate(R.layout.fragment_main, container, false);
        return rootView;
    }
    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        viewPager = view.findViewById(R.id.viewPager);
        tabLayout = view.findViewById(R.id.tabLayout);

        // 设置ViewPager2的适配器
        MainViewPagerAdapter adapter = new MainViewPagerAdapter(requireActivity());
        viewPager.setAdapter(adapter);
        viewPager.setUserInputEnabled(false); // 禁止用户滑动切换页面

        // 添加页面变化监听器
        viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                updateStatusBarForFragment(position);
            }
        });
        new TabLayoutMediator(tabLayout, viewPager, (tab, position) -> {
            switch (position) {
                case 0:
                    tab.setText("首页");
                    break;
                case 1:
                    tab.setText("任务");
                    break;
                case 2:
                    tab.setText("科普");
                    break;
                case 3:
                    tab.setText("百科");
                    break;
                case 4:
                    tab.setText("我的");
                    break;
            }
        }).attach();

        viewPager.setCurrentItem(0);
    }

    private void updateStatusBarForFragment(int position) {
        switch(position) {
            case 0:
                MyApp.updateStatusBarColorAndIcons(true, requireActivity());
                break;
            case 1:
                MyApp.updateStatusBarColorAndIcons(false, requireActivity());
                break;
            case 2:
                MyApp.updateStatusBarColorAndIcons(true, requireActivity());
                break;
            case 3:
                MyApp.updateStatusBarColorAndIcons(false, requireActivity());
                break;
            case 4:
                MyApp.updateStatusBarColorAndIcons(false, requireActivity());
                break;
        }
    }
}